package com.company.card_service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.company.card_service.mapper.EmployeeMapper;
import com.company.card_service.mapper.UserInfoMapper;
import com.company.card_service.model.Employee;
import com.company.card_service.model.Message;
import com.company.card_service.mapper.MessageMapper;
import com.company.card_service.model.UserInfo;
import com.company.card_service.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.card_service.util.PageHelper;
import com.company.card_service.vo.MessageVo;
import com.company.card_service.vo.Result;
import com.company.card_service.vo.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 赵志浩
 * @since 2022-09-15 04:15:07
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {


    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    MessageMapper messageMapper;

    @Override
    public Boolean saveAll(Message message) {
        UserInfo userInfo = userInfoMapper.selectById(message.getUserId());
        Employee employee = employeeMapper.selectById(message.getEmployeeId());
        if(!employee.getPhone().equals(userInfo.getPhone())){
            message.setCreateTime(new Date());
            this.baseMapper.insert(message);
            return true;
        }

            return false;
    }

    /**
     * 通过员工id查询回复留言信息
     * @param eid  employee_id
     * @param pageNum  查询页码
     * @param limit    每页显示条数
     * @return
     */
    @Override
    public Result getMessageById(int eid, int pageNum, int limit) {

        //1.根据员工id查询所有留言数
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("employee_id",eid);
        int count = Math.toIntExact(messageMapper.selectCount(wrapper));
        // 2.计算总页数（必须确定每页显示多少页 pagesize = limit）
        int pageCount =(count%limit == 0)? count/limit : count/limit+1;

        //3.查询当前页的数据（因为留言中需要回复信息，因此需要连表查询----自定义） 查看当前页的数据
        int start =(pageNum-1)*limit;
        List<MessageVo> messageVos = messageMapper.selectMessageVoById(eid,start,limit);

        if (messageVos.size() == 0){
            return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage());
        }else {
            return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage(),new PageHelper<MessageVo>(count,pageCount,messageVos));
        }
    }

    @Override
    public Result getMessageCountById(int eid) {
        //1.查询当前员工名片下所有留言总量
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("employee_id",eid);

        int total = Math.toIntExact(messageMapper.selectCount(wrapper));

        //2.查询不公开留言
        QueryWrapper<Message> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("employee_id",eid)
                .eq("is_public",0);
        int NoPublicTotal = Math.toIntExact(messageMapper.selectCount(wrapper1));

        //3.查询公开留言
        QueryWrapper<Message> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("employee_id",eid)
               .eq("is_public",0);
        int PublicTotal = Math.toIntExact(messageMapper.selectCount(wrapper2));

        //4.查询待回复留言
        QueryWrapper<Message> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("employee_id",eid)
               .eq("is_reply",1);
        int NoReplyTotal = Math.toIntExact(messageMapper.selectCount(wrapper3));

        //4.查询已回复留言
        QueryWrapper<Message> wrapper4 = new QueryWrapper<>();
        wrapper4.eq("employee_id",eid)
               .eq("is_reply",0);
        int ReplyTotal = Math.toIntExact(messageMapper.selectCount(wrapper4));

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("NoPublicTotal",NoPublicTotal);
        map.put("PublicTotal",PublicTotal);
        map.put("NoReplyTotal",NoReplyTotal);
        map.put("ReplyTotal",ReplyTotal);
        return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage(),map);
    }

    @Override
    public Result getNoPublicMessageVoById(int eid,int pageNum,int limit) {
        //1.根据员工id查询不公开留言数
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("is_public",1);
        wrapper.eq("employee_id",eid);
        int count = Math.toIntExact(messageMapper.selectCount(wrapper));
        // 2.计算总页数（必须确定每页显示多少页 pagesize = limit）
        int pageCount =(count%limit == 0)? count/limit : count/limit+1;

        //3.查询当前页的数据（因为留言中需要回复信息，因此需要连表查询----自定义） 查看当前页的数据
        int start =(pageNum-1)*limit;

        List<MessageVo> messageVos = messageMapper.selectNoPublicMessageVoById(eid,start,limit);
        if (messageVos.size() == 0){
            return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage(),null);
        }else {
            return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage(),new PageHelper<MessageVo>(count,pageCount,messageVos));
        }
    }

    public Result getPublicMessageVoById(int eid,int pageNum,int limit) {
        //1.根据员工id查询不公开留言数
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("is_public",0);
        wrapper.eq("employee_id",eid);
        int count = Math.toIntExact(messageMapper.selectCount(wrapper));
        // 2.计算总页数（必须确定每页显示多少页 pagesize = limit）
        int pageCount =(count%limit == 0)? count/limit : count/limit+1;

        //3.查询当前页的数据（因为留言中需要回复信息，因此需要连表查询----自定义） 查看当前页的数据
        int start =(pageNum-1)*limit;

        List<MessageVo> messageVos = messageMapper.selectPublicMessageVoById(eid,start,limit);
        if (messageVos.size() == 0){
            return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage(),null);
        }else {
            return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage(),new PageHelper<MessageVo>(count,pageCount,messageVos));
        }
    }

    @Override
    public Result getNoReplyMessageVoById(int eid,int pageNum,int limit) {
        //1.根据员工id查询待回复留言回复数
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("is_reply",0);
        wrapper.eq("employee_id",eid);
        int count = Math.toIntExact(messageMapper.selectCount(wrapper));
        // 2.计算总页数（必须确定每页显示多少页 pagesize = limit）
        int pageCount =(count%limit == 0)? count/limit : count/limit+1;
        //3.查询当前页的数据（因为留言中需要回复信息，因此需要连表查询----自定义） 查看当前页的数据
        int start =(pageNum-1)*limit;
        List<MessageVo> messageVos = messageMapper.selectNoReplyMessageVoById(eid,start,limit);
        if (messageVos.size() == 0){
            return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage());
        }else {
            return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage(),new PageHelper<MessageVo>(count,pageCount,messageVos));
        }
    }

    @Override
    public Result getReplyMessageVoById(int eid,int pageNum,int limit) {
        //1.根据员工id查询不公开留言回复数
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("is_reply",0);
        wrapper.eq("employee_id",eid);
        int count = Math.toIntExact(messageMapper.selectCount(wrapper));
        // 2.计算总页数（必须确定每页显示多少页 pagesize = limit）
        int pageCount =(count%limit == 0)? count/limit : count/limit+1;

        //3.查询当前页的数据（因为留言中需要回复信息，因此需要连表查询----自定义） 查看当前页的数据
        int start =(pageNum-1)*limit;

        List<MessageVo> messageVos = messageMapper.selectReplyMessageVoById(eid,start,limit);
        if (messageVos.size() == 0){
            return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage(),null);
        }else {
            return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage(),new PageHelper<MessageVo>(count,pageCount,messageVos));
        }
    }

    @Override
    public void removeMessageById(Integer id) {
        if(ObjectUtils.isNotEmpty(id)){
            messageMapper.deleteById(id);
        }
    }
}
